﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using RATSharpW7.ViewModel;
using System.Windows.Navigation;
using System.Windows.Controls.Primitives;
using Coding4Fun.Toolkit.Controls;
using RATSharp.Rest;
using RATSharp.Model;

namespace RATSharpW7.View
{
    /// <summary>
    /// Object browser screen. It's actually just a container for 
    /// loaded objects from
    /// 1) favourites
    /// 2) searched objects
    /// 3) QR code
    /// All these cases are handled here
    /// </summary>
    public partial class ObjectBrowser : PhoneApplicationPage
    {
        /// <summary>
        /// this is static variable representing the selected object.
        /// It´s superimportant to null this variable on every use.
        /// Default navigation service does not allow to put some data during
        /// stepback so we need to reference this data in static context.
        /// </summary>
        private static RATSharp.Model.Object selectedObject { get; set; }

        public static RATSharp.Model.Object SelectedObject {
            get
            {
                RATSharp.Model.Object obj = selectedObject;
                selectedObject = null;
                return obj;
            }
        }

        public static string ScannedQRCode { get; set; }
        AboutPrompt searchPopup;


        bool isNewPageInstance = false;
        public ObjectBrowserViewModel model { get; set; }
        private SearchControl searchControl;

        public ObjectBrowser()
        {
            InitializeComponent();
            isNewPageInstance = true;
        }

        private void FavouriteButton_Click(object sender, EventArgs e)
        {
            model.loadFavourites();
        }

        
        private void qrcodeButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate("View" , typeof(QRScanner), null);
        }


        private void ObjectListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                // get item from ListBox and store in class var. Store page to navigate to in class var.
                RATSharp.Model.Object selectedItem = (RATSharp.Model.Object)e.AddedItems[0];
                //Uri targetPage = new Uri("/View/ObjectProfile.xaml", UriKind.Relative);




                // reset selection of ListBox
                ((ListBox)sender).SelectedIndex = -1;

                // PageTransition.Begin();

                // change  page navigation 
                selectedObject = selectedItem;
                NavigationService.GoBack();
            }
        }



        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

            string value;
            if (isNewPageInstance)
            {
                if (model == null)
                {
                    if (State.ContainsKey("ObjectBrowserViewModel"))
                    {
                        MessageBox.Show("Retrieving state...");
                        model = State["ObjectBrowserViewModel"]
                                           as ObjectBrowserViewModel;
                    }
                    else
                    {
                        model = new ObjectBrowserViewModel();
                    }
                }
                DataContext = model;
            }
            else
            {
                if ((value = ObjectBrowser.ScannedQRCode) != null)
                {
                    //We have found a QR string - don´t forget to null static value
                    ObjectBrowser.ScannedQRCode = null;
                    model.loadQR(value);
                }

            }

            isNewPageInstance = false;
        }

       private void onSearchDialogClosed(object sender, PopUpEventArgs<object, PopUpResult> e)
        {
            if (e.PopUpResult == PopUpResult.Cancelled)
            {
                //dont care
                return;
            }
            SearchBy searchBy = searchControl.selectedSearchBy;
            string searchedText = searchControl.stringSearchTextBox.Text;

            if (String.IsNullOrEmpty(searchedText))
            {
                ToastPrompt toast = new ToastPrompt();
                toast.Title = Application.Current.Resources["SearchByStringCannotBeEmptyLabel"] as string;
                toast.Show();
            }
            else
            {
                model.loadSearchable(searchBy, searchedText);
            }

        }
        private void searchButton_Click(object sender, EventArgs e)
        {
            searchPopup = new AboutPrompt();
            searchPopup.Completed += onSearchDialogClosed;

            searchControl = new SearchControl();
            searchPopup.Body = searchControl;
            searchPopup.Title = Application.Current.Resources["SearchLabel"] as string;
            searchPopup.Show();
        }


        public static bool sel { get; set; }
    }



}